﻿using System.Web.Mvc;
using nanoCMS.Core;
using nanoCMS.Core.WebProviders.HtmlHelpers;
using nanoCMS.Forum.WebUI.Models;
using nanoCMS.Forum.WebUI.Models.ViewModels;

namespace nanoCMS.Forum.WebUI.Controllers
{
    public class ForumController : Controller
    {
        private readonly WorldContext worldContext;
        private ForumService forumService;

        public ForumController(WorldContext worldContext)
        {
            this.worldContext = worldContext;
            this.forumService = new ForumService(worldContext);
        }

        /// <summary>
        /// Displays forum
        /// </summary>
        /// <param name="elementKey"></param>
        /// <param name="forumID"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public ActionResult Index(string elementKey, int forumID = 0, int page = 1)
        {
            ViewBag.ElementKey = elementKey;
            if (forumID == 0)
            {
                return PartialView("Index", forumService.BuildForumIndexModel());
            }
            else
            {
                return PartialView("ForumDisplay", forumService.BuildForumExplorerModel(forumID, page));
            }
        }

        #region Thread actions
        /// <summary>
        /// Opens form for creating thread in forum
        /// </summary>
        /// <param name="forumID"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult CreateThread(int forumID, string elementKey)
        {
            ViewBag.ElementKey = elementKey;
            return PartialView("EditThread", forumService.BuildCreateThreadModel(forumID));
        }

        [HttpGet]
        public ActionResult EditThread(int threadID, string elementKey)
        {
            ViewBag.ElementKey = elementKey;
            return PartialView("EditThread", forumService.BuildEditThreadModel(threadID));
        }

        [HttpPost]
        public ActionResult SaveThread(EditThreadVM model, string elementKey, string link)
        {
            ViewBag.ElementKey = elementKey;
            if (ModelState.IsValid)
            {
                ThreadVM threadResult = null;
                object returnResult = null;
                if (model.Thread.ThreadID == 0)
                {
                    threadResult = forumService.CreateThread(model);
                    // make return result 
                    returnResult = new
                    {
                        status = "redirect",
                        location = LinkHelper.UrlInner(new { link, elementKey, action = "ShowThread", threadId = threadResult.ThreadID }).ToString()
                    };

                }
                else
                {
                    threadResult = forumService.EditThread(model);
                    // make return result 
                    returnResult = new
                    {
                        status = "redirect",
                        location = LinkHelper.UrlInner(new { link, elementKey, action = "ShowThread", threadId = threadResult.ThreadID }).ToString()
                    };
                }
                return Json(returnResult, JsonRequestBehavior.AllowGet);
            }
            return PartialView("ValidationSummary");
        }

        public ActionResult DeleteThread(int threadID, int forumID)
        {
            int fID = forumID; // C# fail // TODO
            forumService.DeleteThread(threadID);
            return RedirectToAction("Index", "Forum", new { forumID });
        }

        public ActionResult ShowThread(string elementKey, int threadID, int page = 1)
        {
            ViewBag.ElementKey = elementKey;
            var model = forumService.BuildThreadDisplayModel(threadID, page);
            if (model != null)
            {
                forumService.IncreaseThreadViews(threadID);
            }
            return PartialView("ThreadDisplay", model);
        }

        public ActionResult ChangeLockThreadState(int threadID)
        {
            forumService.ChangeLockThreadState(threadID);
            ThreadDisplayVM model = forumService.BuildThreadDisplayModel(threadID, 1);
            return PartialView("ThreadDisplay", model);
        }
        #endregion

        #region Post actions
        public ActionResult CreatePost(int threadID, string elementKey)
        {
            ViewBag.ElementKey = elementKey;
            return PartialView("EditPost", forumService.BuildCreatePostModel(threadID));
        }

        public ActionResult EditPost(int postID, string elementKey)
        {
            ViewBag.ElementKey = elementKey;
            return PartialView("EditPost", forumService.BuildEditPostModel(postID));
        }

        // TODO redirect to last post
        public ActionResult SavePost(PostVM post, string elementKey, string link)
        {
            ViewBag.ElementKey = elementKey;
            object returnResult = null;
            if (post.PostID == 0)
            {
                var newPost = forumService.CreatePost(post);
                returnResult = new
                {
                    status = "redirect",
                    location = LinkHelper.UrlInner(new { link, elementKey, action = "ShowThread", threadId = post.ThreadID }).ToString()
                };
            }
            else
            {
                var editedPost = forumService.EditPost(post);
                // make return result 
                returnResult = new
                {
                    status = "redirect",
                    location = LinkHelper.UrlInner(new { link, elementKey, action = "ShowThread", threadId = post.ThreadID }).ToString()
                };
            }
            return Json(returnResult, JsonRequestBehavior.AllowGet);
        }

        public ActionResult DeletePost(int postId, int threadID, string elementKey)
        {
            ViewBag.ElementKey = elementKey;
            forumService.DeletePost(postId);
            var model = forumService.BuildThreadDisplayModel(threadID, 1); // TODO page
            if (model != null)
            {
                forumService.IncreaseThreadViews(threadID);
            }
            return PartialView("ThreadDisplay", model);
        }
        #endregion

        // TODO - move to ForumAdminController
        public ActionResult DeleteForum(int forumID)
        {
            if (ModelState.IsValid)
            {
                forumService.DeleteForum(forumID);
            }
            return RedirectToAction("Index", "Forum");
        }
    }
}